<?php
/*************************************************************************
Login module
----------------------------------------------------------------
Derasoft CMS Project
Company: Derasoft Co., Ltd                                  
Email: info@derasoft.com                                    
Last updated: 02/07/2008
**************************************************************************/
#login
include_once(ROOT_PATH."classes/dao/members.class.php");
include_once(ROOT_PATH."classes/dao/products.class.php");
include_once(ROOT_PATH."classes/dao/vote.class.php");
include_once(ROOT_PATH."includes/functions.php");
$members = new Members();
$products = new Products();
$votes = new Vote();
$templateFile = "";
$vote = $request->element('vote',1);
$idvote = $request->element('idvote','');
$status = array();
$userId = 0;
if(isset($_SESSION['memberId'])){
	$userId = $_SESSION['memberId'];	
};
$objUser = $members->getObject($userId);
if(is_object($objUser)) {
	$objProduct = $products->getObject($idvote);
	if($objProduct){
		$check=$votes->checkUserVoteProduct($idvote,$userId);
		if($check==0){
			$voteinfo = new VoteInfo($idvote, $userId, $vote, 1);
			$fields = array('idproduct'=>$voteinfo->getIdProduct(),
							'iduser'=>$voteinfo->getIdUser(),
							'vote'=>$voteinfo->getVote(),
							'status'=>$voteinfo->getStatus()
							);
			$votes->addData(&$fields);
			// binh chon thanh cong
			$status['error'] = 3;
			// tính diem va lay so lan vote
			$voteProduct=$votes->getVoteProduct($idvote);
			$status['number_vote'] = $voteProduct['solan'];
			$status['sodiem']= $voteProduct['sodiem'];
		}else{
			// da binh chon roi
			$status['error'] = 2;	
		}
	}
}else{
	// chua dang nhap
	$status['error'] = 1;
}
echo json_encode($status);
?>